function [z,more]=neighbor_predict_plot(z,opt,y)

%NEIGHBOR_PREDICT_PLOT graphic display of committee voting results
%Syntax: z=neighbor_predict_plot(z,opt,y)
%Description: z is the seccond ouput argument of neighbor_predict.
%             opt is the switch toggle for the differnt plot functions.
%             y is the actual output, or something else needed for some options
%
% Jonas ALmeida, 22 Feb 2006

if nargin<2
    opt='general';
end

switch opt
    case 'general'
        n=length(z.predict.var(:,1));
        semilogx(100*mean(z.predict.dy)/n,'o','MarkerFaceColor','y');
        %Mark best panel
        [lala,Ind]=min(mean(z.predict.dy));
        hold on
        semilogx(Ind,100*mean(z.predict.dy(:,Ind))/n,'o','MarkerFaceColor','k');
        hold off
        %figure;semilogx(sum(z.predict.err),'o','MarkerFaceColor','y');
        xlabel('number of variables used by each commitee member')
        ylabel('% Wrong Neighbors')
        
        %grid on
        %py=mean(z.y,2); %pooled response
        %[n,m]=size(z.nx);
        % Plot independent parameter values
    case 'members'
        %get comitee membership
        if nargin<3
            [lala,m]=min(mean(z.predict.dy)); %optimal number of variables
        else
            m=y;
        end
        n=length(z.predict.var(:,1));
        for i=1:n
            disp(z.vars(z.predict.var(i,1:m)));
        end
    case 'clusterall'
        double_cluster_dt(z);
    case 'clusterpanel' %cluster only members of the panel, membership can be submited as 3rd input argument
        if nargin<3
            [lala,m]=min(mean(z.predict.dy)); %optimal number of variables
        else
            m=y;
        end
        U=unique(z.predict.var(:,1:m))
        %dt=z;
        dt.vars=z.vars(U);
        dt.nx=z.nx(:,U);
        dt.samples=z.samples;
        dt.y=z.y;
        double_cluster_dt(dt);
    case 'memberpie'
        if nargin<3
            [lala,m]=min(mean(z.predict.dy)); %optimal number of variables
        else
            m=y;
        end
        clear lala
      
        n=length(z.predict.var(:,1));
        for i=1:n
            lala_i='';
            for j=1:m
                lala_i=[lala_i,z.vars{z.predict.var(i,j)},' + '];                
            end
            lala_i(end-2:end)=[];
            %disp(lala_i)
            lala{i}=lala_i;
        end
        L=unique(lala); %labels
        for i=1:length(L);
            C(i)=strmatch(lala{i},L); %count occurrences
        end
        [lala,Ind]=sort(C);
        
        h=pie(C(Ind),L(Ind));title('Distribution of Panel Compositions')
        %legend(L(Ind),-1)
    case 'survival'
        if nargin==3;axes(y);end %<<< what an ugly way to pass object handles :-(
        dy=z;
        n=length(dy.predict);
        N=length(dy.y);
        for i=1:n
            %rebuild dt
            dt=dy;
            dt.predict=dy.predict(i);
            dt.samples(dt.predict.exclude)=[];
            dt.y(dt.predict.exclude)=[];
            dt.x(dt.predict.exclude,:)=[];
            dt.nx(dt.predict.exclude,:)=[];
            %How many patients apply?
            P(i)=N-length(dt.predict.exclude);
            T(i)=dt.predict.T;
            %Survival
            dt.y=dt.y>dt.predict.T;
            S(i)=mean(dt.y);
            %Prediction
            [Err(i),Ind(i)]=min(mean(dt.predict.dy));
            Err(i)=100*Err(i)/P(i);
            
        end
        %figure;
        nf=4;
        subplot(nf,1,4)
        plot(T,Ind,'ob','MarkerSize',4,'MarkerFaceColor','b');ylabel('Panel members  ( \bullet )')
        ax=axis;axis([ax(1)-(ax(2)-ax(1))/6,ax(2),0,ax(4)]);ax4=axis;
        subplot(nf,1,3)
        plot(T,P,':b');ylabel('Samples available  ( - - - )','Rotation',270,'Position',[101 28.1607 1.0001])
        ax=axis;axis([ax(1),ax(2)+(ax(2)-ax(1))/6,0,ax(4)]);ax3=axis;
        %Survival
        subplot(nf,1,2)
        plot(T,Err,'ok','MarkerFaceColor','r');ylabel('% Predictive Error  ( \bullet )')
        ax=axis;axis([ax(1),ax(2),0,ax(4)]);ax2=axis;
        subplot(nf,1,1);
        plot(T,S*100,'-','MarkerFaceColor','r','LineWidth',3,'Color','r');ylabel('% Survival (\fontsize{20} \bf^\_^\_^\_\rm\fontsize{12} )','Rotation',270,'Position',[91 49.4048 1.00011]);xlabel('Progressive Outcome')
        ax=axis;axis([ax(1),ax(2),0,ax(4)]);ax1=axis;

        G=get(gcf);
        G1=get(G.Children(1));
        G2=get(G.Children(2));
        G3=get(G.Children(3));
        G4=get(G.Children(4));
        set(G.Children(1),'Color','none','Box','off','Position',[0.2 0.2 0.6 0.6],'YAxisLocation','right')
        set(G.Children(2),'Color','none','Box','off','Position',[0.2 0.2 0.6 0.6],'YAxisLocation','left','XTick',[])
        set(G.Children(3),'Color','none','Box','off','Position',[0.2 0.2 0.7 0.6],'YAxisLocation','right','XTick',[],'XColor','w')
        set(G.Children(4),'Color','none','Box','off','Position',[0.1 0.2 0.7 0.6],'YAxisLocation','left','XTick',[],'XColor','w')
        
        set(G1.YLabel,'Color','r','FontSize',12);
        set(G2.YLabel,'Color','r','FontSize',12);
        set(G3.YLabel,'Color','b');
        set(G4.YLabel,'Color','b');
        
        %set(G1.YLabel,'Position',[YL.Position(1),ax1(3),YL.Position(3)],'HorizontalAlignment','left')
        %axes(G.Children(1));hold on;plot(YL.Posi(1),YL.Position(2),'o','MarkerFaceColor','r');hold off
        YL=get(G2.YLabel);%set(G2.YLabel,'Position',[YL.Position(1),ax1(3),YL.Position(3)],'HorizontalAlignment','left')
        YL=get(G3.YLabel);%set(G3.YLabel,'Position',[YL.Position(1),ax1(3),YL.Position(3)],'HorizontalAlignment','left')
        YL=get(G4.YLabel);%set(G4.YLabel,'Position',[YL.Position(1),ax1(3),YL.Position(3)],'HorizontalAlignment','left')
        %G1=get(G.Children(1));
        %YL=get(G1.YLabel);set(G1.YLabel,'Rotation',-90,'Position',[ax1(2)*1.08,YL.Position(2:3)])
        %YL=get(G3.YLabel);set(G3.YLabel,'Rotation',-90,'Position',[ax3(2)*1.08,YL.Position(2:3)])
        set(gcf,'Color','w');
    case 'recruit' %recruitment tree for single model
        subplot(1,2,2)
        dt=z;
        if nargin<3%then chose optimal size
           [lala,m]=min(mean(dt.predict.dy)); %optimal number of variables
        else
           m=y;
        end
        %the variables:
        vars=dt.predict.var(:,1:m);Ind=[];
        nvars=length(vars(:));
        maxf=0; %collect maximum frequency
        for i=1:m
            [U{i},ii,jj]=unique(vars(:,i));
            Ind=[Ind,jj];
            %sort U{i} by frequency
            F=[];%frequency
            for j=1:length(U{i})
               F=[F,sum(vars(:,i)==U{i}(j))]; 
            end
            [lala,ii]=sort(-F);
            if max(F)>maxf;maxf=max(F);end
            U{i}=U{i}(ii);
        end
        
        N=round(maxf*1.5);
        for i=1:m %for each additional recruit
            MaxVar=0;
            for j=1:length(U{i})
                plot(j,i,'+k');hold on
                if length(U{i})>MaxVar;MaxVar=length(U{i});end
            end
            %text(0,i,num2str(i))
            if i>1 %connect the dots
                Mam=[1:length(U{i-1})]-1/N;
                Mpm=[1:length(U{i})]-1/N;
                for j=1:length(vars(:,1)) %for th ith member of the jth panel
                    % coming from
                    Iam=find(U{i-1}==vars(j,i-1));Mam(Iam)=Mam(Iam)+1/N;
                    Ipm=find(U{i}==vars(j,i));Mpm(Ipm)=Mpm(Ipm)+1/N;
                    plot([Mam(Iam),Mpm(Ipm)],[i-1,i],'-','Color',[0.6 0.6 0.6]);
                end
            end
            text(0.8,i,num2str(i),'FontSize',7)
        end
        for i=1:m
            mm=length(U{i});
            for j=1:mm
                text(j,i,[' ',dt.vars{U{i}(j)}],'Color','b','FontWeight','bold','Rotation',40,'FontSize',10-5*((i+j)/(m+mm))); %'FontAngle','italic',
            end
        end
        text(MaxVar,1,[num2str(dt.predict.T),'\newlinemonths'],'EdgeColor',[0.75,0.75,0.75],'BackgroundColor',[0.85,0.85,0.85],'HorizontalAlignment','left','FontAngle','italic')
        hold off
        G=get(gcf);
        set(G.Children(1),'YDir','reverse','XScale','log','YScale','log');
        if length(vars(1,:))>1
            axis([0.8,MaxVar,1,length(vars(1,:))])
        end
        axis off
        text(1,m+1,[num2str(length(dt.predict.var(:,1))),' committees (lines), each one with ',num2str(m),'members'])
        %title([num2str(dt.predict.T),'\newlinemonths'],'FontSize',9,'HorizontalAlignment','left')
        
        subplot(1,2,1)
        n=length(z.predict.var(:,1));
        semilogy(100*mean(z.predict.dy(:,1:m))/n,[1:m],':.','MarkerSize',15,'LineWidth',1,'Color','k');%,'MarkerFaceColor','y');
        %Mark best panel
        %[lala,Ind]=min(mean(z.predict.dy));
        %hold on
        %semilogy(100*mean(z.predict.dy(:,Ind))/n,Ind,'o','MarkerFaceColor','k');
        ax=axis;
        axis([ax(1),ax(2),ax(3),m])
        %hold off
        %figure;semilogx(sum(z.predict.err),'o','MarkerFaceColor','y');
        ylabel('Variable recruitment');% \newline ... \newline ...')
        xlabel('% Wrong Classifications')
        G=get(gcf);G1=get(G.Children(1));
        set(G.Children(1),'Color','none','YDir','reverse','XDir','reverse','Box','off','YAxisLocation','right','XAxisLocation','top','YTickLabel','')
        set(G1.YLabel,'rotation',-90,'VerticalAlignment','baseline','FontSize',8);set(G.Children(1),'YTick',[1:m])
        set(G1.XLabel,'FontSize',8)
        %VerticalAlignment: [ top | cap | {middle} | baseline | bottom ]
        grid on
        set(G.Children(1),'Position',[0.1,0.1,0.2,0.8]);set(G.Children(2),'Position',[0.35,0.1,0.55,0.8]);
        set(gcf,'Color','w');
    case 'predict_survival'
        % Individual survival prediction
        x=y;dy=z;clear z
        %Quantile normalization
        %nx=memb(x);
        %For all points in the survival 
        for i=1:length(dy.predict)
            dt=dy;dt.predict=dy.predict(i);
            %disp(i)
            if strcmp(class(dt.y),'double') %then we need to extract the dt variable for a point in time
                dt.x(dt.predict.exclude,:)=[];
                dt.y(dt.predict.exclude,:)=[];
                dt.nx(dt.predict.exclude,:)=[];
                dt.dead(dt.predict.exclude)=[];
                dt.y=dt.y<dt.predict.T;
            end
            [py(i),pc(i)]=neighbor_predict(dt,y); %PY is the predicted classification and PC is the committee decision detail
            
        end        
        more.py=py;more.pc=pc;
        t=[dy.predict(:).T]; % Time points
        z=more.py;
        plot(t,z,'o','MarkerFaceColor',[0 0 0])
        %title('Predicted Mortality')
        xlabel('Months after diagnosis')
        ylabel('Committee voting results')   
end